#include "stdafx.h"
#include "HighResolutionTimer.h"



/*是否使用高精度计数器*/
HighResolutionTimer::HighResolutionTimer(bool isToUseHeigh)
{
	if (isToUseHeigh)
	{
		LARGE_INTEGER large;
		
		if (QueryPerformanceFrequency(&large)) {
			this->frequency = large.QuadPart;
		}
		else {
			this->frequency = 0;
		}
	}

	else
	{
		this->frequency = 0;
	}
}

HighResolutionTimer::~HighResolutionTimer()
{
	this->frequency = 0;
}

/*获取当前精度计数器时间*/
__int64 HighResolutionTimer::getNow()
{
	if (this->frequency == 0)
	{
		return GetTickCount();
	}
	else
	{
		LARGE_INTEGER counter;

		if (!QueryPerformanceCounter(&counter))
		{
			return GetTickCount();
		}

		return counter.QuadPart;
	}

}
/*获取计数器*/
__int64 HighResolutionTimer::getCounter(__int64 ms)
{
	if (this->frequency == 0)
	{
		return ms;
	}

	return (ms * this->frequency) / 1000;
}

/*获取毫秒数*/
__int64 HighResolutionTimer::getMS(__int64 counter)
{
	if (this->frequency == 0)
	{
		//硬件不支持 高精度计数器

		return counter;
	}
	
	return static_cast<__int64>((counter *1.0 / this->frequency)* 1000);
}
